package com.vincent.kdp.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vincent.baseapp.base.BaseFragment;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.Activity.CheckGoodsActivity;
import com.vincent.kdp.Activity.CounterManagerActivity;
import com.vincent.kdp.Activity.CustomerActiviy;
import com.vincent.kdp.Activity.LoginActivity;
import com.vincent.kdp.Activity.TankActivity;
import com.vincent.kdp.Activity.WaitConfirmListActivity;
import com.vincent.kdp.ui.other.MemberSearchActivity;
import com.vincent.kdp.Activity.NoticeActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.adapter.CenterModuleAdapter;
import com.vincent.kdp.ui.fragment.contract.CenterContract;
import com.vincent.kdp.ui.fragment.presenter.CenterPresenter;
import com.vincent.kdp.ui.other.MainActivity;

import butterknife.Bind;

/**
 * Created by Administrator on 2016/5/28.
 */
public class CenterFragment extends BaseFragment<CenterContract.View, CenterPresenter> implements CenterContract.View {


    @Bind(R.id.recycleView)
    RecyclerView mRecycleView;
    @Bind(R.id.center_rl)
    RelativeLayout mCenterRl;
    @Bind(R.id.tv_name)
    TextView ttv;
    @Bind(R.id.rl_tvname)
    TextView rltvname;
    @Bind(R.id.yuangongnumber)
    TextView yuangongnumber;
    @Bind(R.id.img_name)
    TextView imgName;

    private CenterModuleAdapter mAdapter;
    private View settingLayout;
    private ImageButton qc;

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_center;
    }

    @Override
    protected void initViewData() {
        presenter.initView(null);
        initRecycleView();
    }


    private void initRecycleView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 1);
        mRecycleView.setLayoutManager(gridLayoutManager);
        mAdapter = new CenterModuleAdapter(getActivity(), mOnItemClicker);
        mRecycleView.setAdapter(mAdapter);
        String username = SPUtil.getString("name");
        String usernumber = SPUtil.getString("username");
        ttv.setText(username);
        imgName.setText(username);
        rltvname.setText(username);
        yuangongnumber.setText(usernumber);
    }

    private CenterModuleAdapter.OnItemClicker mOnItemClicker = new CenterModuleAdapter.OnItemClicker() {

        //实现对recyleview每一项的监听
        @Override
        public void operate(View view, int position) {
            switch (position) {
                case 0:
                    // 通知信
                    startActivity(new Intent(getActivity(), NoticeActivity.class));
                    break;
                case 1:
                    //今日盘货
                    startActivity(new Intent(getActivity(), CheckGoodsActivity.class));
                    break;
                case 2:
                    //今日收货
                    startActivity(new Intent(getActivity(), WaitConfirmListActivity.class));
                    break;
                case 3:
                    //意向客户
                    startActivity(new Intent(getActivity(), CustomerActiviy.class));
                    break;
                case 4:
                    //会员消费
                    startActivity(new Intent(getActivity(), MemberSearchActivity.class));
                    break;
                case 5:
                    //柜台管理
                    startActivity(new Intent(getActivity(), CounterManagerActivity.class));
                    break;
                case 6:
                    logout();
                    break;
            }
        }
    };

    private void logout(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("是否退出登录?");
        builder.setTitle("提示");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                SPUtil.remove("username");
                startActivity(new Intent(getContext(),LoginActivity.class));
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.setCancelable(false);
        builder.create().show();
    }

    @Override
    public void setLoginViewVisible(boolean visible) {
        mCenterRl.setVisibility(visible ? View.GONE : View.VISIBLE);
        ((MainActivity) getActivity()).setToolbarCenterTextView(visible ? "员工登录" : "更多");
    }

    @Override
    public String getAccount() {
        return null;
    }

    @Override
    public CenterPresenter initPresenter() {
        return new CenterPresenter();
    }

    @Override
    public void onResume() {
        super.onResume();
        mAdapter.notifyDataSetChanged();
    }
}
